﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CTEM.Controllers.BUS;
using CTEM.Controllers.Global;
using CTEM.Models;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

namespace CTEM.ModuleForms.ManageRole
{
    public partial class DXSetRight : DevExpress.XtraEditors.XtraForm
    {
        readonly List<RoleDTO> _listRole;
        readonly List<RightDTO> _listRights;
        List<RightDTO> _listRightsOfCurrentRole;
        long _roleId;
        long _rightValue;
        readonly RoleBUS _roleBus = new RoleBUS();
        readonly RightBUS _rightBus = new RightBUS();
        public DXSetRight(List<RoleDTO> listRole)
        {
            
            _listRole = listRole;
            _listRights = _rightBus.LoadAllRight(); 
            InitializeComponent();
            rightDTOBindingSource.DataSource = _listRights;
            roleDTOBindingSource.DataSource = _listRole;
            lookUpEdit1.Properties.BestFitMode = BestFitMode.BestFitResizePopup;
            lookUpEdit2.Properties.BestFitMode = BestFitMode.BestFitResizePopup;

        }
        public DXSetRight(long roleID, long rightValue, List<RoleDTO> listRole)
        {
           
        }
        private  void  LoadRightsOfCurrentRole ()
        {
            lbxRights.DataSource = _listRightsOfCurrentRole;
            lbxRights.DisplayMember = Constants.RIGHT_DISPLAYMEMBER;
            lbxRights.ValueMember = Constants.RIGHT_VALUEMEMBER;
        }
        private long RightValueCaculate()
        {
            long rightsValue = 0;
            foreach (var right in _listRightsOfCurrentRole)
            {
                rightsValue += right.Value;
            }
            return rightsValue;
        }

        private void lookUpEdit1_EditValueChanged(object sender, EventArgs e)
        {
            var currRole = lookUpEdit1.GetSelectedDataRow() as RoleDTO;
            if (currRole != null)
            {
                _roleId = currRole.RoleID;
                _rightValue = currRole.RightsValue;
                _listRightsOfCurrentRole = _rightBus.GetRightByRole(currRole.RightsValue);
            }
            
            LoadRightsOfCurrentRole();
        }

        private void btnAddRight_Click(object sender, EventArgs e)
        {
            var currRole = lookUpEdit1.GetSelectedDataRow() as RoleDTO;
            var currRight = lookUpEdit2.GetSelectedDataRow() as RightDTO;
            if(currRole!=null)
            {
                if (currRight != null)
                { 
                    foreach (var item in _listRightsOfCurrentRole)
                        {                        
                                if (item.RightName.Equals(currRight.RightName))
                                {
                                    MessageBox.Show("Quyền Bạn vừa thêm đã tồn tại.");
                                    return;
                                }                            
                        }
                    _listRightsOfCurrentRole.Add(currRight);  
                    currRole.RightsValue = RightValueCaculate();
                    _roleBus.EditRole(currRole);
                    LoadRightsOfCurrentRole();
                }
                else
                {
                    MessageBox.Show("Bạn phải chọn Quyền trước khi thêm.", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                MessageBox.Show("Bạn phải chọn Chức Vụ trước.", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }

        private void bntDeleteRight_Click(object sender, EventArgs e)
        {
            var currRole = lookUpEdit1.GetSelectedDataRow() as RoleDTO;
            var currRight = lbxRights.SelectedItem as RightDTO;
            if(currRole != null)
            {
               _listRightsOfCurrentRole.Remove(currRight);
                if (currRole != null)
                {
                    currRole.RightsValue = RightValueCaculate();
                    _roleBus.EditRole(currRole);
                }
                LoadRightsOfCurrentRole(); 
            }
        }
    }
}